package com.xinjian.jianba.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.MonsDto;
import com.xinjian.jianba.dto.MonsIdDto;
import com.xinjian.jianba.dto.MonsVo;
import com.xinjian.jianba.dto.UserMonmentsDto;
import com.xinjian.jianba.dto.UserMonmentsVo;
import com.xinjian.jianba.dto.remindVo;
import com.xinjian.jianba.exception.Page;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.SiteListService;
import com.xinjian.jianba.service.UserMonmentsService;
import com.xinjian.jianba.service.ValidatorService;

import com.xinjian.jianba.utils.JsonUtil;
import com.xinjian.jianba.utils.LogBuilder;
import com.xinjian.jianba.utils.ModuleEnum;


@RestController
@RequestMapping(value="/api/v3/jianba/monments")
public class UserMonmentsController extends AbstractController {
	private static final Logger LOGGER = LoggerFactory.getLogger(UserMonmentsController.class);
	@Autowired
	private SiteListService siteListService;
	@Autowired 
	private UserMonmentsService userMonmentsService;
	@Autowired
	private ValidatorService validatorService;
	@PostMapping(value="/release")
	public String release(@RequestBody UserMonmentsDto userMonmentsDto){
	LOGGER.info(LogBuilder.build(ModuleEnum.Mons, "用户发布朋友圈,参数 userMonmentsDto={}"), JsonUtil.getJsonString(userMonmentsDto));
		try {
			validatorService.valid(userMonmentsDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		UserMonmentsVo userMonmentsVo=null;
		try {
			 userMonmentsVo=siteListService.saveSiteListAndUserMonments(userMonmentsDto);
		} catch (Exception e) {
			LOGGER.error("--------error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, e.getMessage());
		}
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, userMonmentsVo);
		
	}
	@PostMapping(value="/mons")
	public String mons(@RequestBody MonsDto monsDto){
		LOGGER.info(LogBuilder.build(ModuleEnum.Mons, "获取好友朋友圈,参数 monsDto={}"), JsonUtil.getJsonString(monsDto));
		try {
			validatorService.valid(monsDto);
			Map<String,Object> map=userMonmentsService.getMons(monsDto);
			map.put("code",  ResponseCode.SUCCESS+"");
			map.put("message",ResponseCode.SUCCESS_MSG);
			String resultStr = JsonUtil.getJsonString(map);
			return resultStr;
		} catch (Exception e) {
			LOGGER.error("--------error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		
		
	}
	@PostMapping(value="/mons/id")
	public String monsId(@RequestBody MonsIdDto monsDto) throws ParseException{
		LOGGER.info(LogBuilder.build(ModuleEnum.Mons, "查看某个人的朋友圈,参数 monsDto={}"), JsonUtil.getJsonString(monsDto));
		try {
			validatorService.valid(monsDto);
		} catch (Exception e) {
			LOGGER.error("--------error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		List<MonsVo> list=userMonmentsService.getPersonMons(monsDto);
		
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, list);
	}
	@PostMapping(value="/mon/delete")
	public String monDelete(@RequestBody Map<String, String> map){
		LOGGER.info(LogBuilder.build(ModuleEnum.Mons, "删除朋友圈,参数 Map={}"), JsonUtil.getJsonString(map));
		 Long monId;
		 String address=null;
		if(map.containsKey("monId")){
           monId=Long.parseLong(map.get("monId"));
        }else{
        	return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "monId不能为空");
        }
		if(map.containsKey("address")){
			address=map.get("address");
		}else{
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "没传address字段");
		}
		int k=userMonmentsService.delete(monId,address);
		if(k==1){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		}else{
			int count=userMonmentsService.findCountByMonId(monId);
			if(count>0){
				return this.responseClientJsonData(ResponseCode.ERROR, "删除失败");
			}else{
				return this.responseClientJsonData(ResponseCode.SUCCESS, "不要重复删除");
			}
		}
	}
	@PostMapping(value="/remind")
	public String remind(@RequestBody Map<String,String> map) throws NumberFormatException, ParseException{
		String userId=map.get("userId");
		LOGGER.info(LogBuilder.build(ModuleEnum.Mons, "查看未读消息,参数 userId={}"), userId);
		if(userId==null||userId.equals("")){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "用户id不能为空"); 
		}
		List<remindVo> list=userMonmentsService.remindDetailAndUpdate(Long.parseLong(userId));
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, list);
	}
	@PostMapping(value="/monId")
	public String getByMonId(@RequestBody Map<String,String> map) throws ParseException{
		LOGGER.info(LogBuilder.build(ModuleEnum.Mons, "根据朋友圈id,用户id查询朋友圈,参数 Map={}"), JsonUtil.getJsonString(map));
		String monId=map.get("monId");
		if(monId==null||monId.equals("")){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "monId不能为空"); 
		}
		String userId=map.get("userId");
		if(userId==null||userId.equals("")){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "userId不能为空");
		}
		Map<String,Object> resultMap=userMonmentsService.findMonsByMonId(monId,userId);
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		list.add(resultMap);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, list);
	}
}
